package com.api.sw.duban.action;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.sw.duban.model.Uf_jcdcrw;
import com.api.sw.duban.model.Uf_jcgzsd;
import com.api.sw.duban.service.Uf_jcdcrwBaseService;
import com.api.sw.duban.util.DubUtil;
import com.api.sw.duban.util.LogTool;
import com.api.sw.duban.util.UploadWord;
import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.*;
import com.cloudstore.eccom.result.WeaResultMsg;
import weaver.conn.RecordSet;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;

import java.text.SimpleDateFormat;
import java.util.*;

/*
    决策督查 action
 */
public class DubanJcImportAction {
    private final static LogTool log = new LogTool("/log/duban/",false);
    public DubanJcImportAction(){

    };
    //查询条件  文号 标题 编号
    public Map<String, Object> getTablelist(Map<String,Object> params, User user) {
        boolean flag=true;
        String wfkey=(System.currentTimeMillis()+(int)(Math.random() * (9999-1000) + 1000))+"";
        String mess="";
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            Uf_jcdcrw uf_jcdcrw=null;
            List<Uf_jcdcrw> listobj=new ArrayList<Uf_jcdcrw>();//用于创建 流程对象
            List<Object> listsql=new ArrayList<Object>();//用于创建 流程对象 写入数据库
            RecordSet re=new RecordSet();
            String tzid=(String)Util.null2String(params.get("tzid")).trim() ;//通知ID
            String rwid=(String)Util.null2String(params.get("rwid")).trim() ;//任务ID
            if(rwid.endsWith(",")){
                rwid=rwid.substring(0,rwid.length()-1);
            }

            UploadWord UploadWord=new UploadWord();
            UploadWord.setRoot(listobj,tzid,wfkey);//设置根目录
            Uf_jcgzsd uf_jcgzsd =UploadWord.getUf_jcgzsdBytzid(tzid);
            String sql="select * from uf_jcdcrw where id in ("+rwid+") and zt in (0,1,2,3) and sfsrw=0";//只取任务
            log.writeLog(sql);
            re.execute(sql);
            long newDomid=System.currentTimeMillis()+(int)(Math.random() * (9999-1000) + 1000);
            //封装数据
            while(re.next()){
                uf_jcdcrw=new Uf_jcdcrw();
                String bh=Util.null2String(re.getString("bh"));
                String gzrw=Util.null2String(re.getString("gzrw"));
                String qtdw=Util.null2String(re.getString("qtdw"));
                String lb=Util.null2String(re.getString("lb"));
                //String zt=Util.null2String(re.getString("bh"));
                String ssdctz=Util.null2String(re.getString("ssdctz"));
                String sssj=Util.null2String(re.getString("sssj"));
                String levelids=Util.null2String(re.getString("levelids"));
                String domid=Util.null2String(re.getString("domid"));
                //setListDomid(List<Uf_jcdcrw> list,String mainDomid,String newDomid,String wfkey,int level,String yssdctz,String newssdctz ){
                String newLeveldomid=DubUtil.setListDomid(listsql,domid,newDomid+"",wfkey,uf_jcgzsd.getMaxcel(),ssdctz,tzid,user);
                if("".equals(newLeveldomid)){
                    throw new Exception("生成Domid异常");
                }
                log.writeLog("newLeveldomid"+newLeveldomid);
                //拼接字符
                newLeveldomid=newLeveldomid.substring(0,newLeveldomid.lastIndexOf("-")+1)+newDomid;
                String newsssj=newLeveldomid.split("-")[newLeveldomid.split("-").length-2];
                uf_jcdcrw.setSsdctz(tzid);
                uf_jcdcrw.setLb(lb);
                uf_jcdcrw.setGzrw(gzrw);
                uf_jcdcrw.setDomid(newDomid+"");
                uf_jcdcrw.setLevelids("1-"+newLeveldomid);
                uf_jcdcrw.setSssj(newsssj);
                uf_jcdcrw.setSfsrw(0);
                uf_jcdcrw.setWfkey(wfkey);
                uf_jcdcrw.setQtdw(qtdw);
                uf_jcdcrw.setZt(0);
                listobj.add(uf_jcdcrw);
                newDomid++;
            }
            log.writeLog(listobj.toString());
            log.writeLog(listsql.toString());
            if(listsql!=null&&listsql.size()>0){
                //插入 数据库
                Uf_jcdcrwBaseService Uf_jcdcrwBaseService=new Uf_jcdcrwBaseService();
                String errormess=Util.null2String(Uf_jcdcrwBaseService.getObjMapList(listsql,null)) ;
                if(!"".equals(errormess)){
                    throw new Exception(""+errormess);
                }

            }
            if(listobj!=null&&listobj.size()>0){
                //UploadWord UploadWord=new UploadWord();
                String errormess= UploadWord.CreateWorflow(user,listobj);
                if(!"".equals(errormess)){
                    throw new Exception(""+errormess);
                }

            }

        } catch (Exception e) {
            mess="fail-"+e.getMessage();
            log.writeLog("fail-"+e.getMessage());
            e.printStackTrace();

        }
        if(!"".equals(mess)){
            flag=false;
        }
        apidatas.put("flag",flag);
        apidatas.put("mess",mess);
        return apidatas;
    }

}
